"""
公共接口
"""
import os
from conf import settings
from db import modols


def get_all_school_interface():
    school_dir = os.path.join(settings.DB_PATH, 'school')

    if not os.path.exists(school_dir):
        return False, None

    school_list = os.listdir(school_dir)
    return True, school_list


def get_course_list_in_school(school_name):
    school_obj = modols.School.select(school_name)
    course_list = school_obj.course_list
    return course_list


def login_interface(username, password, user_type):
    if user_type == 'admin':
        user_obj = modols.Admin.select(username)
    elif user_type == 'student':
        user_obj = modols.Student.select(username)
    elif user_type == 'teacher':
        user_obj = modols.Teacher.select(username)
    else:
        return False, '登录角色错误'

    if not user_obj:
        return False, '用户不存在！'
    if user_obj.password == password:
        return True, '登录成功！'
    else:
        return False, '密码错误'
